<template>
  <section>
    <a-affix class="sub-navbar draft">
      <a-button type="primary" :loading="isSubmitLoading" @click="handleSubmit">保存 </a-button>
    </a-affix>
    <div class="app-container is-form">
      <a-form :form="form">
          ## for(item in entity.generalList) {
          ## if(item.isId == true){continue;}
          ## if(item.name == "deleted" ){continue;}
          ## if(item.name == "createAt" ){continue;}
          ## if(item.name == "creatorId" ){continue;}
          ## if(item.name == "modifyAt" ){continue;}
          ## if(item.name == "modifierId" ){continue;}
          <a-form-item v-bind="formItemLayout" label="${item.displayName}">
            <a-input v-decorator="['${item.name}',{ rules: [{ required: true, message: '请输入${item.displayName}' }] }]" />
          </a-form-item>
          ## }
      </a-form>
    </div>
  </section>
</template>
<script>
  import {saveOne,getDetail} from "../../api/${entity.code}";
  export default {
    name:"${entity.code}",
    data() {
      return{
        pageTitle: '新增${entity.displayName}',
        formItemLayout:{
          labelCol: {
            xs: { span: 24 },
            sm: { span: 4 },
          },
          wrapperCol: {
            xs: { span: 24 },
            sm: { span: 16 },
          },
        },
        loading: false,
        isSubmitLoading:false,
        form: this.$form.createForm(this),
        model:{}
      }
    },
    methods: {
      handleSubmit() {
        this.form.validateFieldsAndScroll(async (err, values) => {
          if (!err) {
            let entity = {...this.model,...values}
            let resp = await saveOne({entity:entity})
            this.isSubmitLoading = false;
            if(this.qUtil.validResp(resp)){
              this.$message.success("${entity.displayName}信息已保存");
              this.$store.dispatch('delCurrentViews',{
                view: this.$route,
                $router: this.$router
              })
            }
          }else{
            this.isSubmitLoading = false;
          }
        });
      },
      async loadModel(){
        let id = this.$route.query.id || 0
        if(id > 0){
          let resp = await getDetail(id)
          if(this.qUtil.validResp(resp)){
            this.pageTitle = "编辑${entity.displayName}";
            this.model = this.baseUtil.modelCopy(resp.data.model);
            const {
              ## for(item in entity.generalList) {
                ## if(item.name == "id" ){continue;}
                ## if(item.name == "deleted" ){continue;}
                ## if(item.name == "createAt" ){continue;}
                ## if(item.name == "creatorId" ){continue;}
                ## if(item.name == "modifyAt" ){continue;}
                ## if(item.name == "modifierId" ){continue;}
                ${item.name},
              ## }
            } = resp.data.model;

            this.form.setFieldsValue({
              ## for(item in entity.generalList) {
              ## if(item.name == "deleted" ){continue;}
                  ## if(item.name == "createAt" ){continue;}
                  ## if(item.name == "creatorId" ){continue;}
                  ## if(item.name == "modifyAt" ){continue;}
                  ## if(item.name == "modifierId" ){continue;}
                ${item.name},
              ## }
            });
          }
        }
      },
    },
    mounted() {
      this.loadModel()
    }
  }
</script>
